package com.romaap.pokedex;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class PokedexActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onClickButton(View v)
    {
            	new AlertDialog.Builder(this)
        	            .setItems(R.array.select_dialog_items,
        	                    new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface arg0,
											int arg1) {
										ImageView imageview = (ImageView) findViewById(R.id.image);
								    	String imageURL = "http://assets1.pokemon.com/static/_ui/img/sections/pokedex/full/" + (arg1<9 ? "0" : "") + (arg1<99 ? "0" : "") + (arg1+1) + ".png";
								    	Drawable image = ImageOperations(imageURL);
								        imageview.setImageDrawable(image);
									}
        	                    })
        	            .setPositiveButton("jeej",
        	                    new DialogInterface.OnClickListener() {
        	                public void onClick(DialogInterface dialog, int whichButton) {

        	                    /* User clicked Yes so do some stuff */
        	                }
        	            })
        	            .setNegativeButton("neej",
        	                    new DialogInterface.OnClickListener() {
        	                public void onClick(DialogInterface dialog, int whichButton) {

        	                    /* User clicked No so do some stuff */
        	                }
        	            })
        	           .show();
    }
    
    private Drawable ImageOperations(String url) {
		try {
			InputStream is = (InputStream) this.fetch(url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
    
    public Object fetch(String address) throws MalformedURLException,IOException {
		URL url = new URL(address);
		Object content = url.getContent();
		return content;
	}
}